<!DOCTYPE html>
<html>

<head>

    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">


    <title>生态瞬时测试</title>
    <meta name="keywords" content="">
    <meta name="description" content="">

    <link rel="shortcut icon" href="favicon.ico">
    <link href="../css/bootstrap.min.css?v=3.3.6" rel="stylesheet">
    <link href="../css/font-awesome.css?v=4.4.0" rel="stylesheet">
    <link href="../css/plugins/iCheck/custom.css" rel="stylesheet">
    <link href="../css/animate.css" rel="stylesheet">
    <link href="../css/style.css?v=4.1.0" rel="stylesheet">

</head>

<body class="gray-bg">
<div class="wrapper wrapper-content animated fadeInRight">

    <div class="row">
        <div class="col-sm-12">
            <div class="ibox float-e-margins">
                <div class="ibox-title">
                    <h5>实验名称/被试者管理</h5>
                </div>
                <div class="ibox-content">
                    <div class="row">
                        <div class="col-sm-5 m-b-xs"></div>
                        <div class="col-sm-4 m-b-xs"></div>
                        <div class="col-sm-3">
                            <a href="add-metadata.html" class="btn btn-sm btn-primary">添加元数据</a>
                            <a href="add-experimental-item.html" class="btn btn-sm btn-primary">添加被试</a>
                        </div>
                    </div>
                    <div class="table-responsive">
                        <table class="table table-bordered">
                            <thead>
                            <tr>

                                <th></th>
                                <th>用户名</th>
                                <th>真实姓名</th>
                                <th>生日</th>
                                <th>联系方式</th>

                                <th>操作</th>
                            </tr>
                            </thead>
                            <tbody id="subject-box">

                            </tbody>
                        </table>
                    </div>

                </div>
            </div>
        </div>

    </div>
</div>

<!-- 全局js -->
<script src="../js/jquery.min.js?v=2.1.4"></script>
<script src="../js/bootstrap.min.js?v=3.3.6"></script>
<!-- iCheck -->
<script src="../js/plugins/iCheck/icheck.min.js"></script>
<!--日期转换-->
<script src="../js/dateChange.js"></script>
<script>
    $(document).ready(function () {
        $('.i-checks').iCheck({
            checkboxClass: 'icheckbox_square-green',
            radioClass: 'iradio_square-green',
        });
    });
</script>



<script>
    $(document).ready(function () {
        var testId = sessionStorage.getItem("testId");
        var testName = sessionStorage.getItem("testName");
        $(".ibox-title").eq(0).html(` <h5>${testName}/被试者管理</h5>`);
        // console.log(testId);
        var token = sessionStorage.getItem("token");
        $.ajax({
            type:"GET",
            headers:{
                authToken:token
            },
            url:"http://123.206.18.189:8080/experiments/"+testId+"/experiment_subjects",
            success:function (res) {
                console.info(res)
                var html = "";
                for(var i=0;i<res.length;i++){
                    var day = dateChange(res[i].birthday);
                    html += `
                        <tr alt="${res[i].id}">
                                    <td></td>
                                    <td>${res[i].userName}</td>
                                    <td>${res[i].realName}</td>
                                    <td>${day}</td>
                                    <td>${res[i].telephone}</td>

                                    <td>
                                        <i class="fa fa-trash-o del" title="删除" style="margin-right: 20px;"></i>
                                        <i class="fa fa-eye look" attr-id="${res[i].userId}" title="查看" style="margin-right: 20px;"></i>
                                    </td>
                                </tr>
                    `
                }
                $("#subject-box").html(html);
            }

        })
        //查看
        $("#subject-box").on("click",".look",function(){
            var userId = $(this).attr("attr-id");
            location.href="./strategy-time.html?userId=" + userId;
        });

        //删除被试者
        $("#subject-box").on("click",".del",function () {
            var subjectId = $(this).parent().parent().attr("alt");
            var r = confirm("是否要删除该被试者！");
            if(r){
                $.ajax({
                    type:"DELETE",
                    url:"http://123.206.18.189:8080/experiments/"+testId+"/experiment_subjects/"+subjectId,
                    headers:{
                        authToken:token
                    },
                    complete:function (XMLHttpRequest) {

                        if(XMLHttpRequest.status){
                            alert("被试者已删除！");
                            window.location.reload();
                        }else{
                            alert("删除失败！错误信息"+XMLHttpRequest.status)
                        }

                    }
                })
            }

        })
    })
</script>


</body>

</html>
